<?php
class NewsAction extends BaseAction{
	public function addNews(){
		$this->assign("position" ,"新闻添加");
		if ( $this->isPost() ) {
			$data['title'] = addslashes(trim($_POST['title']));
			$data['content'] = trim($_POST['contents']);
			$data['type'] = addslashes(trim($_POST['type']));
			$data['subtype'] = addslashes(trim($_POST['subtype']));
			$data['times'] = addslashes(trim($_POST['time']));
			$result = M("news")->data($data)->add();
			if ( $result ) {
				$this->assign("jumpUrl","__URL__/checkNews");
				$this->assign("waitSecond",3);
				$this->success("新闻添加成功！！！");
			} else {
				$this->assign("jumpUrl","__URL__/addNews");
				$this->assign("waitSecond",3);
				$this->error("新闻添加失败！！！");
			}
		} else {
			$newCategory = M("newstype")->order("id desc")->select();
			$this->assign("newscategory",$newCategory);
			$newsub = M("news_subtype")->select();
			$this->assign("newsubtype",$newsub);
			$this->display();
		}
	}
	
	public function checkNews(){
		$this->assign("position" ,"新闻查看");
		import("ORG.Util.Page");
		$count   = M("news")->count();
		$Page    = new Page($count,6);
		$show    = $Page->show();
		$allNews = M("news")->field("news.id,newstype.type_name,news.title,news.content,news.times,news_subtype.new_s_name")->join("newstype ON news.type = newstype.id")->join("news_subtype ON news.subtype = news_subtype.new_s_id")->order("times desc,id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
		
		
		//$allNews = M("news")->join("newstype ON news.type = newstype.id")->order("times desc")->select();
		$this->assign("news",$allNews);
		$this->assign("page",$show);
		$this->display();
	}
	
	public function delNews(){
		if ( $_POST['id'] ) {
			$result = M("news")->where("id=".$_POST['id'])->delete();
			if ( $result ){
				echo "success";
			}else {
				echo "failure";
			}
		} else {
			echo "failure";
		}
	}
	
	public function editNews(){
		$this->assign("position" ,"新闻修改");
		if ( $this->isPost() ) {
			$data['title'] = addslashes(trim($_POST['title']));
			$data['content'] = trim($_POST['contents']);
			$data['type'] = addslashes(trim($_POST['type']));
			$data['times'] = addslashes(trim($_POST['time']));
			$data['new_s_id'] = addslashes(trim($_POST['new_s_id']));
			$result = M("news")->data($data)->where("id=".$_POST['id'])->save();
			if ( $result ){
				$this->assign("jumpUrl","__URL__/checkNews");
				$this->assign("waitSecond",3);
				$this->success("新闻修改成功！！！");
			} else {
				//$this->assign("jumpUrl","__URL__/checkNews");
				$this->assign("waitSecond",3);
				$this->error("新闻失败成功！！！");
			}		
		} else {
			if ( $_GET['id']){
				$newsInfo = M("news")->field("news.id,newstype.type_name,news.title,news.content,news.times,news.type,news_subtype.new_s_name")->join("newstype ON news.type = newstype.id")->join("news_subtype ON news.subtype = news_subtype.new_s_id")->where("news.id = ".$_GET['id'])->find();
				$this->assign("newsinfo" ,$newsInfo);
				$newCategory = M("newstype")->order("id desc")->select();
				$this->assign("newscategory",$newCategory);
				$newsub = M("news_subtype")->select();
				$this->assign("new_subtype",$newsub);
				$this->display();
			} else {
				$this->assign("waitSecond",3);
				$this->error("新闻失败成功！！！");
			}
		}
	}
}